package com.amigo.online.provider.user.controller;

import com.amigo.online.common.base.security.LocalUser;
import com.amigo.online.common.core.response.Result;
import com.amigo.online.common.core.response.StatusCode;
import com.amigo.online.common.core.security.VipCheck;
import com.amigo.online.common.param.dto.activity.AddActivityDto;
import com.amigo.online.common.param.result.activity.ActivityInfo;
import com.amigo.online.common.util.UploadTempUtil;
import com.amigo.online.provider.user.common.GlobalController;
import com.amigo.online.provider.user.exception.UserException;
import com.amigo.online.provider.user.mapper.ActivityCoverTblMapper;
import com.amigo.online.provider.user.mapper.ActivityMapper;
import com.amigo.online.provider.user.mapper.ActivityParticipateTblMapper;
import com.amigo.online.provider.user.mapper.ActivityTblMapper;
import com.amigo.online.provider.user.model.entity.ActivityCoverTbl;
import com.amigo.online.provider.user.model.entity.ActivityReadTbl;
import com.amigo.online.provider.user.service.ActivityService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import org.checkerframework.checker.units.qual.A;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.validation.constraints.NotNull;
import java.io.IOException;
import java.util.List;


/**
 * 活动
 * @author pangdonghao
 * @version 1.0
 * @csdn https://blog.csdn.net/pangdongh
 * @createDate 2019/11/06 15:03
 */
@Api("活动")
@RestController
@RequestMapping(value = "/activity")
public class ActivityController {
    @Autowired
    ActivityService activityService;
    @Autowired
    ActivityTblMapper activityTblMapper;
    @Autowired
    ActivityParticipateTblMapper activityParticipateTblMapper;
    @Autowired
    ActivityMapper activityMapper;
    @Autowired
    ActivityCoverTblMapper activityCoverTblMapper;


    /**
     * 添加活动
     * @return
     */
    @ApiOperation(value = "添加活动" ,  notes="添加活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @PostMapping()
    public Result add(AddActivityDto addActivityDto){
        if(addActivityDto.getVideo()!=null && addActivityDto.getDuration()==null){//有视频必须有视频的毫秒数
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        if(addActivityDto.getVideo()!=null && addActivityDto.getPhotos()!=null){//图片和视频不能同时存在
            throw new UserException(StatusCode.BAD_REQUEST);
        }
        if(addActivityDto.getContent()==null){//描述不能为空
            throw new UserException(StatusCode.BAD_REQUEST);
        }

        activityService.add(addActivityDto);
        return Result.success();
    }

    @ApiOperation(value = "删除活动" ,  notes="删除活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @DeleteMapping("/{id}")
    public Result delete(@PathVariable("id")  Long id){

        int boo = activityMapper.deleteById(id,LocalUser.getUser().getUser().getId());
        if(boo == 1){
            return Result.success();
        }else {
            throw new UserException(StatusCode.BAD_REQUEST);
        }
    }

    @ApiOperation(value = "获取某人发布的活动" ,  notes="获取某人发布的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("getActivityByUserId/{userId}/{page}")
    public Result<List<ActivityInfo>> getActivityByUserId(@PathVariable("userId")  Long userId,@PathVariable("page")  Integer page){
        return Result.success(activityService.getActivityByUserId(userId, page));
    }

    @ApiOperation(value = "获取最近发布的活动" ,  notes="获取某人发布的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("/{page}")
    public Result<List<ActivityInfo>> getActivity(@PathVariable("page")  Integer page){
        return activityService.getActivity(page);
    }


    @ApiOperation(value = "参加活动" ,  notes="参加活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("participateActivity/{activityId}")
    public Result participateActivity(@PathVariable("activityId")  Long id){
        return activityService.participateActivity(id);

    }

    @ApiOperation(value = " 取消参加活动" ,  notes="取消参加活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("cancelParticipateActivity/{activityId}")
    public Result cancelParticipateActivity(@PathVariable("activityId")  Long id){
        activityService.cancelParticipateActivity(id);
        return Result.success();
    }


    @ApiOperation(value = "查自己参加的活动" ,  notes="查自己参加的活动")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("getActivitiesIParticipated/{page}")
    public Result<List<ActivityInfo>> getActivitiesIParticipated(@PathVariable("page")  Integer page){
        return Result.success(activityService.getActivitiesIParticipated(page));
    }

    //========================================  封面  ==================================================

    @ApiOperation(value = " 添加/修改封面" ,  notes="添加/修改封面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
            @ApiImplicitParam(name = "image", value = "封面图片",paramType = "form",required = true),
    })
    @VipCheck
    @PostMapping("cover")
    @Transactional(rollbackFor=Exception.class)
    public Result cover(@RequestPart(value = "image")  MultipartFile image){
        return Result.success(activityService.cover(image));
    }

    @ApiOperation(value = " 获取封面" ,  notes="获取封面")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
            @ApiImplicitParam(name = "image", value = "封面图片",paramType = "form",required = true),
    })
    @VipCheck
    @GetMapping("cover")
    @Transactional(rollbackFor=Exception.class)
    public Result<String> getCover(){
        return Result.success(activityService.getCover());
    }


    //========================================  已读未读  ==================================================

    @ApiOperation(value = "获取已读最新ID" ,  notes="获取已读最新ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("read")
    public Result<Long> getRead(){
        return Result.success(activityService.getRead());
    }

    @ApiOperation(value = "更新已读最新ID" ,  notes="更新已读最新ID")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @PutMapping("read/{readId}")
    public Result putRead(@PathVariable(value = "readId")  Long readId){
        activityService.putRead(readId);
        return Result.success();
    }

    @ApiOperation(value = "获取未读活动条数" ,  notes="获取未读条数")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "ACCESS_TOKEN", value = "用户token",paramType = "header",required = true),
    })
    @VipCheck
    @GetMapping("unreadNum")
    public Result unreadNum(){
        return Result.success(activityService.unreadNum());
    }

}
